package org.jeecg.modules.flowable.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.modules.flowable.common.util.IdUtil;
import org.jeecg.modules.flowable.common.util.json.ObjectConvert;
import org.jeecg.modules.flowable.common.util.validation.AssertValid;
import org.jeecg.modules.flowable.common.util.validation.MustParamValidation;
import org.jeecg.modules.flowable.entity.TActActionEntity;
import org.jeecg.modules.flowable.mapper.TActActionMapper;
import org.jeecg.modules.flowable.service.ActionManagerService;
import org.jeecg.modules.flowable.service.ITActActionService;
import org.jeecg.modules.flowable.service.bo.ActAction;
import org.jeecg.modules.flowable.system.core.SystemUser;
import org.jeecg.modules.flowable.system.etc.SystemUserUtil;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Title:  <br>
 * Desc:  <br>
 * Date: 6/14/22 1:51 PM <br>
 * Company: 联通（辽宁）产业互联网有限公司 <br>
 *
 * @author zxd
 * @version 1.0.0
 */
@Service
public class ActionManagerServiceImpl implements ActionManagerService {

    @Autowired
    private ITActActionService iTActActionService;
    @Autowired
    private TActActionMapper tActActionMapper;


    @Override
    public List<TActActionEntity> queryActionList(String argAtionType) {

        QueryWrapper<TActActionEntity> queryWrapper = new QueryWrapper<TActActionEntity>();
        if(AssertValid.isNotEmpty(argAtionType))
            queryWrapper.eq("ACTION_TYPE",argAtionType);
        queryWrapper.eq("DEL_FLAG",0);
        queryWrapper.orderByAsc("CREATE_TIME");
        List<TActActionEntity> tActActionEntities = tActActionMapper.selectList(queryWrapper);
        return tActActionEntities;
    }

    @Override
    public TActActionEntity save(ActAction argActAction) {
        SystemUser loginUser = SystemUserUtil.getSystemUser();
        DateTime now = DateTime.now();
        MustParamValidation.mustValidation(argActAction.getActionCode(),argActAction.getActionName(),argActAction.getActionType(),argActAction.getActionUrl());
        TActActionEntity convert = ObjectConvert.convert(argActAction, TActActionEntity.class);
        if(AssertValid.isEmpty(argActAction.getId()) || argActAction.getId().equals("-1")) {
            // 新增逻辑
            convert.setId(IdUtil.getStringId());
            convert.setDelFlag(0);
            convert.setCreateBy(loginUser.getRealname());
            convert.setCreateTime(now.toDate());
            convert.setUpdateBy(loginUser.getRealname());
            convert.setUpdateTime(now.toDate());
            convert.setIsForm(0);
            convert.setFormContent("");
            tActActionMapper.insert(convert);
            return convert;
        } else {
            // 修改逻辑
            QueryWrapper<TActActionEntity> queryWrapper = new QueryWrapper<TActActionEntity>();
            queryWrapper.eq("ID",convert.getId());

            convert.setCreateBy(null);
            convert.setCreateTime(null);
            convert.setUpdateBy(loginUser.getRealname());
            convert.setUpdateTime(now.toDate());
            tActActionMapper.update(convert,queryWrapper);
            TActActionEntity tActActionEntity = tActActionMapper.selectById(convert.getId());
            return tActActionEntity;
        }
    }

    @Override
    public void deleteAction(String argId) {
        SystemUser loginUser = SystemUserUtil.getSystemUser();
        DateTime now = DateTime.now();
        MustParamValidation.mustValidation(argId);
        QueryWrapper<TActActionEntity> queryWrapper = new QueryWrapper<TActActionEntity>();
        queryWrapper.eq("ID",argId);
        TActActionEntity convert = new TActActionEntity();
        convert.setDelFlag(1);
        convert.setUpdateBy(loginUser.getRealname());
        convert.setUpdateTime(now.toDate());
        tActActionMapper.update(convert,queryWrapper);
    }

    @Override
    public void saveDesigner(String argId, String argContent) {
        MustParamValidation.mustValidation(argId);
        SystemUser loginUser = SystemUserUtil.getSystemUser();
        DateTime now = DateTime.now();
        TActActionEntity tActActionEntity = new TActActionEntity();
        if(AssertValid.isNotEmpty(argContent)) {
            // 有设计
            tActActionEntity.setId(argId);
            tActActionEntity.setUpdateBy(loginUser.getRealname());
            tActActionEntity.setUpdateTime(now.toDate());
            tActActionEntity.setIsForm(1);
            tActActionEntity.setFormContent(argContent);
        } else {
            // 清空设计
            tActActionEntity.setId(argId);
            tActActionEntity.setUpdateBy(loginUser.getRealname());
            tActActionEntity.setUpdateTime(now.toDate());
            tActActionEntity.setIsForm(0);
            tActActionEntity.setFormContent("");
        }
        QueryWrapper<TActActionEntity> queryWrapper = new QueryWrapper<TActActionEntity>();
        queryWrapper.eq("ID",argId);
        tActActionMapper.update(tActActionEntity,queryWrapper);

    }
}